﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.mainmenu {
    import flash.events.*;
    import org.robotlegs.core.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.views.components.mainmenu.*;
    import com.adobe.air.net.events.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;

    public class FriendsTabMediator extends Mediator implements IMediator {

		[Inject]
        public var tabDisplay:FriendsTabDisplay;
		[Inject]
        public var friendsModel:FriendsModel;
		[Inject]
        public var gameStateModel:GameStateModel;
		[Inject]
        public var userModel:UserModel;
		[Inject]
        public var languageModel:LanguageModel;
        private var _tabLockCount:int;// = 0
        private var _firstTime:Boolean = true;

        override public function onRegister():void{
            eventMap.mapListener(this.tabDisplay, ViewCompEvent.CLICK, this.tabDisplay_click);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_SCROLL_ITEM, this.disableScrollItem);
            eventMap.mapListener(eventDispatcher, EventConstants.GO_HOME_BUTTON_CLICKED, this.goHomeButtonClicked);
            eventMap.mapListener(eventDispatcher, EventConstants.ENABLE_FRIENDS_MENU, this.enabledFriendsMenu);
            eventMap.mapListener(eventDispatcher, EventConstants.DISABLE_FRIENDS_MENU, this.disableFriendsMenu);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_OPEN_CUSTOMIZE_MENU, this.disableFriendsMenu);
            eventMap.mapListener(eventDispatcher, EventConstants.ON_CLOSE_CUSTOMIZE_MENU, this.enabledFriendsMenu);
            eventMap.mapListener(eventDispatcher, EventConstants.SCROLL_TO_ID_IN_FRIENDS_MENU, this.scrollToId);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_HIDDEN_BIRD_FROM_FRIENDS_MENU, this.removeHiddenBird);
            eventMap.mapListener(eventDispatcher, EventConstants.REMOVE_SYMBOL_FROM_FRIENDS_MENU, this.removeSymbol);
            eventMap.mapListener(eventDispatcher, SessionModelEvent.ON_FRIENDS_CHANGED, this.onFriendsChanged);
            eventMap.mapListener(eventDispatcher, CommandConstants.ADD_FRIENDS, this.onAddFriends);
            this.tabDisplay.addInviteBoxes(this.languageModel.getTextByKey("friends_invite"));
        }
        private function removeHiddenBird(_arg1:Event):void{
            this.tabDisplay.removeHiddenBird(this.userModel.visitingFriendID);
        }
        private function removeSymbol(_arg1:Event):void{
            this.tabDisplay.removeSymbol(this.userModel.visitingFriendID);
        }
        private function scrollToId(_arg1:InfoEvent):void{
            this.tabDisplay.scrollToId(_arg1.id);
        }
        private function disableFriendsMenu(_arg1:Event):void{
            this._tabLockCount++;
            l(("c disableFriendsMenu: " + this._tabLockCount));
            this.tabDisplay.mouseChildren = (this.tabDisplay.mouseEnabled = false);
            eventMap.unmapListener(this.tabDisplay, ViewCompEvent.CLICK, this.tabDisplay_click);
        }
        private function enabledFriendsMenu(_arg1:Event):void{
            this._tabLockCount--;
            l(("c enabledFriendsMenu: " + this._tabLockCount));
            if (this._tabLockCount <= 0){
                this._tabLockCount = 0;
                this.tabDisplay.mouseChildren = (this.tabDisplay.mouseEnabled = true);
                eventMap.mapListener(this.tabDisplay, ViewCompEvent.CLICK, this.tabDisplay_click);
            };
        }
        private function goHomeButtonClicked(_arg1:Event):void{
            this.tabDisplay.setMeSelected(this.userModel.mySocialID);
        }
        private function disableScrollItem(_arg1:InfoEvent):void{
            this.tabDisplay.setFriendItemcontrols(_arg1.id);
        }
        private function tabDisplay_click(_arg1:ViewCompEvent):void{
            var _local2:String;
            if (_arg1.sender == GameConstants.INVITE_FRIEND){
                dispatch(new Event(EventConstants.SHOW_FRIEND_INVITATION_WINDOW));
            } else {
                _local2 = _arg1.sender;
                dispatch(new InfoEvent(EventConstants.VISIT_FRIEND, _local2));
            };
        }
        private function onFriendsChanged(_arg1:SessionModelEvent):void{
            this.addFriends();
        }
        private function onAddFriends(_arg1:Event):void{
            if (((!(this.tabDisplay.isHidden())) || (this._firstTime))){
                this.addFriends();
                this._firstTime = false;
            };
        }
        private function addFriends():void{
            var _local1:int;
            var _local5:Object;
            var _local6:String;
            var _local7:Boolean;
            var _local8:Boolean;
            var _local9:Boolean;
            var _local10:String;
            var _local11:Boolean;
            var _local12:String;
            var _local13:int;
            var _local2:Array = this.friendsModel.friends;
            var _local3:uint = _local2.length;
            var _local4:int;
            this.tabDisplay.removeAllFriends();
            if (this.userModel.isVisitFriendsMode()){
                _local12 = this.userModel.visitingFriendID;
            } else {
                _local12 = this.userModel.mySocialID;
            };
            if (_local3 < this.tabDisplay.numberOfVisibleItems){
                _local1 = (this.tabDisplay.numberOfVisibleItems - _local3);
                _local13 = 0;
                while (_local13 < _local3) {
                    _local5 = _local2[_local13];
                    _local6 = ((_local5.picture) || (GameConstants.FACEBOOK_DEFAULT_PIC));
                    _local11 = (_local5.uid == this.userModel.mySocialID);
                    _local7 = (_local5.uid == _local12);
                    _local8 = _local5.bird;
                    _local9 = _local5.symbolToHarvest;
                    _local10 = _local5.symbol;
                    this.tabDisplay.addFriend(_local5.uid, _local5.level, _local5.xp, _local6, _local5.first_name, this.getTooltip(_local11), _local7, _local8, _local9, _local10);
                    _local13++;
                };
                _local13 = 0;
                while (_local13 < _local1) {
                    this.addInviteBox();
                    _local13++;
                };
            } else {
                _local13 = 0;
                while (_local13 < _local3) {
                    _local5 = _local2[_local13];
                    _local6 = ((_local5.picture) || (GameConstants.FACEBOOK_DEFAULT_PIC));
                    _local11 = (_local5.uid == this.userModel.mySocialID);
                    _local7 = (_local5.uid == _local12);
                    _local8 = _local5.bird;
                    _local9 = _local5.symbolToHarvest;
                    _local10 = _local5.symbol;
                    if ((((_local13 > 0)) && (((_local13 % 5) == 0)))){
                        this.addInviteBox();
                        _local4++;
                    };
                    this.tabDisplay.addFriend(_local5.uid, _local5.level, _local5.xp, _local6, _local5.first_name, this.getTooltip(_local11), _local7, _local8, _local9, _local10);
                    _local4++;
                    _local13++;
                };
                _local1 = (this.tabDisplay.numberOfVisibleItems - (_local4 % 6));
                _local13 = 0;
                while (_local13 < _local1) {
                    this.addInviteBox();
                    _local13++;
                };
            };
            this.tabDisplay.scrollToId(this.userModel.visitingFriendID);
        }
        private function getTooltip(_arg1:Boolean):String{
            var _local2:String = (_arg1) ? "visitfriend_display_gohome_tooltip" : "friends_friendsbox_tooltip";
            return (this.languageModel.getTextByKey(_local2));
        }
        private function addInviteBox():void{
            this.tabDisplay.addInviteBox(this.languageModel.getTextByKey("friends_invite"));
        }

    }
}//package net.wooga.pets.views.mainmenu 
